'use strict'

var userName= document.querySelector('input#uesrname');
var inputRoom= document.querySelector('input#room');
var btnConnect= document.querySelector('button#connect');
var outputArea= document.querySelector('textarea#output');
var inputArea= document.querySelector('textarea#input');
var btnSend= document.querySelector('button#send');


var socket ;
var room;
btnConnect.onclick=()=>{
	console.log('发送链接ws事件开始');
	//链接服务器"http://localhost:3100/easemob",{path: "/http://socket.io/"}
  socket = io.connect();
  console.log('开始链接ws',socket);
	//断开服务器
	socket.on('disconnect', socket=> {
		console.log("disconnect===>"+socket);
	})
	//消息
	socket.on('joined',(room,id)=>{
		btnConnect.disabled=true;
		inputArea.disabled=false;
		btnSend.disabled=false;
	});
	socket.on('leaved',(room,id)=>{
		btnConnect.disabled=false;
		inputArea.disabled=true;
		btnSend.disabled=true;
	});
	socket.on('message',(room,id,data)=>{
		console.log("data====>"+data);
		outputArea.scrollTop = outputArea.scrollHeight;//窗口总是显示最后的内容
		outputArea.value=outputArea.value+data+'\r';
	});
	//发送消息
	room =inputRoom.value;
	socket.emit('join',room);
}

btnSend.onclick = () => {
  console.log('发送消息');
	if (inputArea.value==='') {
		return;
	} 
	var data=inputArea.value;
	data=userName.value+':'+data;
	socket.emit('message',room,data);
	outputArea.scrollTop = outputArea.scrollHeight;//窗口总是显示最后的内容
	outputArea.value=outputArea.value+data+'\r';
	inputArea.value='';
}
